﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yunwei.DAL;
using Yunwei.Domain.Models;
using Yunwei.IBizLogic;

namespace Yunwei.BizLogic
{
    public class OrderService : BaseService<Order>, IOrderService
    {
        public OrderService() : base(RepositoryFactory.OrderRepository) { }
        public IQueryable<Order> FindList(int number, string orderNo, int restaurant_Id, int user_Id, DateTime? fromDate, DateTime? toDate, int orderCode)
        {
            return FindPageList(out number, 1, number, orderNo, restaurant_Id, user_Id, fromDate, toDate, orderCode);
        }

        public IQueryable<Order> FindPageList(out int totalRecord, int pageIndex, int pageSize, string orderNo, int restaurant_Id, int user_Id, DateTime? fromDate, DateTime? toDate, int orderCode)
        {
            IQueryable<Order> _commonModels = CurrentRepository.Entities;
            if (!string.IsNullOrEmpty(orderNo)) _commonModels = _commonModels.Where(cm => cm.OrderNo.Contains(orderNo));
            if (restaurant_Id > 0) _commonModels = _commonModels.Where(o => o.Restaurant_Id == restaurant_Id);
            if (user_Id > 0) _commonModels = _commonModels.Where(o => o.User_Id == user_Id);

            if (fromDate != null) _commonModels = _commonModels.Where(cm => cm.OrderTime >= fromDate);
            if (toDate != null) _commonModels = _commonModels.Where(cm => cm.OrderTime <= toDate);

            _commonModels = Order(_commonModels, orderCode);
            totalRecord = _commonModels.Count();
            return PageList(_commonModels, pageIndex, pageSize).AsQueryable();
        }

        public IQueryable<Order> Order(IQueryable<Order> entitys, int orderCode)
        {
            switch (orderCode)
            {
                //默认排序
                default:
                    entitys = entitys.OrderByDescending(cm => cm.OrderTime);
                    break;
            }
            return entitys;
        }
    }
}
